<?php
namespace app\api\controller;

use think\Controller;
use think\Db;

class Index extends Controller
{
	public function index()
	{

		if($this->checkSign()){
			if(input('echostr')){
				echo input('echostr');exit;
			}else{
				$this->log(input(''));
				$this->response();
			}
		}

		exit;
	}

	public function response()
	{
		file_put_contents('./xx.txt',$GLOBALS['HTTP_RAW_POST_DATA']);

		$postStr = $GLOBALS['HTTP_RAW_POST_DATA'];


		$postObj = simplexml_load_string($postStr,'SimpleXMLElement', LIBXML_NOCDATA);

		switch ($postObj->MsgType) {
			case 'text':
				$this->responText($postObj);
				break;
			case 'image':
				$this->responImages($postObj);
				break;
			case 'voice':
				$this->responText();
				break;
			case 'location':
				$this->responText();
				break;
			default:
				$this->responText();
				break;
		}

		$this->log(['data'=>$postObj->Content]);
	}

	public function responText($postObj)
	{

		$text = $this->robots($postObj->Content);
		
		$str = "<xml><ToUserName><![CDATA[".$postObj->FromUserName."]]></ToUserName><FromUserName><![CDATA[".$postObj->ToUserName."]]></FromUserName><CreateTime>".time()."</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[".$text."]]></Content></xml>";

        exit($str);
	}

	public function responImages($postObj)
	{
		$str = "<xml><ToUserName><![CDATA[".$postObj->FromUserName."]]></ToUserName><FromUserName><![CDATA[".$postObj->ToUserName."]]></FromUserName><CreateTime>".time()."</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[".$postObj->MediaId."]]></MediaId></Image></xml>";

        exit($str);
	}

    public function checkSign()
    {
    	$signature = input('signature');
        $timestamp = input('timestamp');
        $nonce = input('nonce');
        $token = "xbs";

        $tmpArr = array($token, $timestamp, $nonce);

        sort($tmpArr);

        $tmpStr = implode($tmpArr);

        $tmpStr = sha1($tmpStr);
        
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

    public function log($data)
    {
    	$res = Db::name('log')->insert(['ctime'=>time(),'content'=>json_encode($data,JSON_UNESCAPED_UNICODE)]);
    }

    public function show()
    {
    	$this->robots("你好");

    	$res = Db::name('log')->order('lid desc')->select();

    	var_dump($res);

    }

    public function robots($text)
    {

		$url = "http://openapi.tuling123.com/openapi/api/v2";

		$data =[
			'reqType'=>0,
			'perception'=>[
				'inputText'=>
				['text'=>$text]
			],
			'userInfo'=>[
				'apiKey'=>'facc47b9f3afc7a493121bf6aacab53f',
				'userId'=>3423
			]
		];


		$curl = curl_init();
		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");//post
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));//post 数据 
		curl_setopt($curl, CURLOPT_HTTPHEADER, array(
		    'Content-Type: application/json',
		    'Content-Length: ' . strlen(json_encode($data))
		));
		curl_setopt($curl, CURLOPT_FAILONERROR, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_HEADER, false);

		$data = curl_exec($curl);

		

		return (json_decode($data,true)['results'][0]['values']['text']);

    }
}
